package com.imonsoft.common.httpModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * easyui使用的tree模型
 * 
 * @author LiQianHui
 * @date 2013-6-4
 * @version 2013
 */
public class TreeGridNode implements java.io.Serializable {

	private static final long serialVersionUID = -1164466456045857589L;
	private String id;
	private String text;// 树节点名称
	private String icon;// 前面的小图标样式
	private String url;
	private String rowNo;
	private String parentId;
	private Boolean checked = false;// 是否勾选状态
	private List<TreeGridNode> children;// 子节点
	private String state = "open";// 是否展开(open,closed)
	private Map<String, String> attributes;// 其他参数

	static final String ROOT_ID = "";

	public static List<TreeGridNode> getTreeGridNodeList(List<TreeGridNode> list, List<TreeGridNode> allList) {
		if (list == null) {
			return new ArrayList<TreeGridNode>();
		}
		List<TreeGridNode> resultList = getTreeGridNodeList(allList);
		
		return resultList;
	}

	public static List<TreeGridNode> getTreeGridNodeList(List<TreeGridNode> list) {
		if (list == null) {
			return new ArrayList<TreeGridNode>();
		}
		List<TreeGridNode> resultList = new ArrayList<TreeGridNode>();
		for (TreeGridNode gridNode : list) {
			if (!exists(gridNode.getParentId(), list)) {
				resultList.add(gridNode);
			}
		}
		List<TreeGridNode> toDo = new ArrayList<TreeGridNode>();
		for (TreeGridNode treeGridNode : resultList) {
			toDo.add(treeGridNode);
		}
		while (!toDo.isEmpty()) {
			TreeGridNode node = toDo.remove(0);
			for (TreeGridNode gridNode : list) {
				if (gridNode.parentId.equals(node.id)) {
					if (node.children == null) {
						node.children = new ArrayList<TreeGridNode>();
					}
					node.children.add(gridNode);
					toDo.add(gridNode);
				}
			}
		}
		return resultList;
	}

	private static boolean exists(String parentId, List<TreeGridNode> list) {
		for (TreeGridNode node : list) {
			if (node.getId().equals(parentId))
				return true;
		}
		return false;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	public List<TreeGridNode> getChildren() {
		return children;
	}

	public void setChildren(List<TreeGridNode> children) {
		this.children = children;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getRowNo() {
		return rowNo;
	}

	public void setRowNo(String rowNo) {
		this.rowNo = rowNo;
	}

	public Map<String, String> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, String> attributes) {
		this.attributes = attributes;
	}

}
